import cv2 as cv
from common.config import config_option

opencv_haarcascades_file = "D:\\directory\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml"

"""
视频人脸检测
"""


def face_detect_demo(src):
    # 图片转换灰度图片
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    # 加载特征数据
    face_detector = cv.CascadeClassifier(opencv_haarcascades_file)
    #
    faces = face_detector.detectMultiScale(gray, scaleFactor=1.02, minNeighbors=5)
    print(faces)
    for x, y, w, h in faces:
        cv.rectangle(src, (x, y), (x + w, y + h), color=(0, 0, 255))
    cv.imshow('result', src)


cap = cv.VideoCapture(config_option['project_path'] + "/data/face_detect/v1.mp4")
while True:
    flag, frame = cap.read()
    print(flag, frame.shape)
    face_detect_demo(frame)
    cv.imshow('result', frame)
    if ord('q') == cv.waitKey(10):
        break
cv.destroyAllWindows()
cap.release()

